# -*- coding: utf-8 -*-
import random
import time
import requests
from bs4 import BeautifulSoup
import pymysql

conn = pymysql.connect('127.0.0.1', 'root', '123456', 'temp', charset='utf8')
cursor = conn.cursor()
sql = 'insert into py09_ip(host,port) values(%s,%s)'
data = ()


def test_ip(proxy):
    headers2 = {
        'Referer': 'http://www.baudi.com/',
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36X-DevTools-Emulate-Network-Conditions-Client-Id: 633649CF613EBC10270B39A490C621E9'
    }
    try:
        proxies = {"http":proxy}
        test_url = "http://www.baidu.com/"
        response = requests.get(test_url, headers=headers2, proxies=proxies, timeout=3)
        return response.status_code
    except :
        pass

def to_text(proxy):

    print("☺☺☺☺☺☺☺☺☺☺☺☺☺biubiu连接成功：{}可用写入中☺☺☺☺☺☺☺☺☺☺☺☺☺".format(proxy))
    proxy = proxy.replace('http://', '')
    proxy0 = proxy.split(':')

    host = proxy0[0].strip()
    port = proxy0[1].strip()
    sql = 'insert into py09_ip(host,port) values(%s,%s)'
    data = (host,port)
    cursor.execute(sql, data)
    conn.commit()

    # with open("ip.csv","a",encoding="utf-8") as f:
    #     proxy = proxy.replace('http://','')
    #     proxy = proxy.replace(':',',')
    #     f.write(proxy+"\n")
    #     f.close()

def get_ip(i):
    headers1 = {
        'Referer': 'http://www.66ip.cn/1.html',
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36'
    }
    url= 'http://www.66ip.cn/'+str(i)+'.html'
    response = requests.get(url,headers=headers1)
    soup = BeautifulSoup(response.content.decode("gbk"),"lxml")
    trs= soup.find("div",class_="containerbox boxindex").find_all("tr")
    for tr in trs[1:]:
        ip = tr.find_all("td")[0].get_text()
        port = tr.find_all("td")[1].get_text()
        proxy = "http://"+ip+":"+port
        status = test_ip(proxy)
        if status == 200:
            to_text(proxy)
        else:
            print("☻☻☻☻☻☻☻☻☻☻☻☻☻链接失败：{}不可用☻☻☻☻☻☻☻☻☻☻☻☻☻".format(proxy))
    # time.sleep(random.randint(3, 4))

if __name__ == '__main__':
    for i in range(1, 101):
        get_ip(i)